<template>
  <div>
    <!--卡片 -->
    <el-card class="messages-container">
      <el-row>
        <el-col :span="8">
          <el-button
            type="warning"
            icon="el-icon-plus"
            size="small"
            @click="handleAdd"
            >添加留言</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table stripe :data="messages">
        <el-table-column type="index" label="序号" prop="id"></el-table-column>
        <el-table-column
          label="留言内容"
          prop="content"
          width="400px"
        ></el-table-column>
        <el-table-column
          label="发表时间"
          prop="created_time"
          width="200px"
        ></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scoped">
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="delmessage(scoped.row.id)"
            >
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器
        current-page: 当前的页码
       -->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pages"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="num"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    <el-dialog title="添加留言" width="40%" :visible.sync="dialogVisible">
      <el-form>
        <el-form-item>
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            placeholder="请输入留言内容"
            v-model="content"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">提交</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Message',
  data() {
    return {
      messages: [
        /* { id: 1, content: '留言11' },
        { id: 2, content: '留言22' }, */
      ],
      num: 5, //每页显示的记录条数
      pages: 1, //当前页码
      total: 0,
      content: '',
      dialogVisible: false, //对话框默认隐藏
    }
  },
  created() {
    this.getMessages()
  },
  methods: {
    getMessages() {
      this.$http
        .get('/messages', {
          params: { pages: this.pages, num: this.num },
        })
        .then((res) => {
          /*  console.log(res) */
          const { code, result } = res
          if (code == 0) {
            this.messages = result.result
            this.total = result.total
          }
        })
    },
    handleSizeChange(num) {
      //当改变每页显示的记录数会执行回调,会把选中的页码作为参数拿过来
      this.num = num
      this.getMessages()
    },
    handleCurrentChange(pages) {
      this.pages = pages
      this.getMessages()
    },
    handleAdd() {
      this.dialogVisible = true
    },
    onSubmit() {
      /*  console.log(this.content) */ 11111
      //  {content: '1111'}
      this.$http.post('/messages', { content: this.content }).then((res) => {
        this.$message.success('添加成功')
        //1.关闭对话框
        this.dialogVisible = false
        //2.重新加载数据
        this.getMessages()
      })
    },
    delmessage(id) {
      //删除数据
      // console.log(id)
      //三个参数，第一个参数和第二个参数为字符串，第三个为选择项(相当于resolve和reject),确定则执行then中的方法，如果取消，执行catch
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.$http.delete('/messages/' + id).then((res) => {
          const { code, msg, result } = res
          if (code == 0) {
            this.$message.success('删除成功')
            this.getMessages()
          }
        })
      })
    },
  },
}
</script>

<style scoped>
.messages-container {
  display: flex;
  justify-content: center;
  /* margin-top: 20px; */
  /* box-shadow: 10px 10px 5px #888888; */
}
.el-table {
  width: 800px;
  margin-top: 20px;
}
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
